package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopBrowseMapper;
import com.ztgf.shopping.models.bo.shop.ShopBrowseBO;
import com.ztgf.shopping.models.bo.shop.ShopBrowseSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopBrowseDTO;
import com.ztgf.shopping.models.dto.shop.ShopCollectDTO;
import com.ztgf.shopping.models.service.shop.IShopBrowseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.models.vo.shop.ShopBrowseVO;
import com.ztgf.shopping.models.vo.shop.ShopCollectVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户足迹  服务实现类
 * </p>
 *
 * @author zhanghualang
 * @since 2020-05-26
 */
@DubboService
public class ShopBrowseServiceImpl extends ServiceImpl<ShopBrowseMapper, ShopBrowseDTO> implements IShopBrowseService {
    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    @Override
    public boolean add(ShopBrowseSaveBO saveBO) {
        //判断足迹是否今天已经有了
        Wrapper<ShopBrowseDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", saveBO.getShopId()).eq("commodity_id", saveBO.getCommodityId());
        Date nowDate = DateUtils.getDayStart(DateUtils.getCurrentDate()); //今日时间
        wrapper.ge("create_time", nowDate);
        List<ShopBrowseDTO> dtoList = this.selectList(wrapper);
        if (dtoList.size() > 0){
            //已经浏览过了
            return true;
        }else {
            ShopBrowseDTO shopBrowseDTO = new ShopBrowseDTO();
            BeanUtils.copyProperties(saveBO,shopBrowseDTO);
            Long id = ymlConfig.getPkId();
            shopBrowseDTO.setId(id);
            return this.insert(shopBrowseDTO);
        }
    }

    @Override
    public HashMap<String, ArrayList<ShopBrowseVO>> queryPageList(Page<ShopBrowseDTO> page, ShopBrowseBO shopBrowseBO) {
        HashMap<String, ArrayList<ShopBrowseVO>> map = new HashMap<String, ArrayList<ShopBrowseVO>>();

        Wrapper<ShopBrowseDTO> wrappern_now = new EntityWrapper<>(); //今天
        Wrapper<ShopBrowseDTO> wrappern_yest = new EntityWrapper<>(); //昨天
        Wrapper<ShopBrowseDTO> wrappern_earlier = new EntityWrapper<>();//更早（小于昨天）

        if(shopBrowseBO.getShopId() != null){
            wrappern_now.eq("shop_id", shopBrowseBO.getShopId());
            wrappern_yest.eq("shop_id", shopBrowseBO.getShopId());
            wrappern_earlier.eq("shop_id", shopBrowseBO.getShopId());
        }

        Date current_date = DateUtils.getDayStart(DateUtils.getCurrentDate()); //今日
        Date yest_date = DateUtils.getDayStart(DateUtils.getYesterdayDate()); //昨天

        //获取今天所有数据
        wrappern_now.ge("create_time", current_date);//>=
        wrappern_now.orderBy("create_time", false);//降序
        Page<ShopBrowseDTO> page_now = this.selectPage(page, wrappern_now);
        List<ShopBrowseDTO> shopBrowseDTOList_now =  page_now.getRecords();
        ArrayList<ShopBrowseVO> shopBrowseVOArrayList_now = new ArrayList<ShopBrowseVO>();

        for (ShopBrowseDTO dto_now : shopBrowseDTOList_now){
            ShopBrowseVO shopBrowseVO_new = new ShopBrowseVO();
            BeanUtils.copyProperties(dto_now,shopBrowseVO_new);
            if (dto_now.getCommodityId() != null){
                shopBrowseVO_new.setCommodityPriceVO(iCommodityInfoService.getByIdOtherInterface(dto_now.getCommodityId()));
                shopBrowseVOArrayList_now.add(shopBrowseVO_new);
            }
        }
        map.put("current", shopBrowseVOArrayList_now);

        //获取昨天所有的
        wrappern_yest.lt("create_time", current_date);//<
        wrappern_yest.ge("create_time", yest_date);//>=
        wrappern_yest.orderBy("create_time", false);//降序
        Page<ShopBrowseDTO> page_yest = this.selectPage(page, wrappern_yest);
        List<ShopBrowseDTO> shopBrowseDTOList_yest =  page_yest.getRecords();
        ArrayList<ShopBrowseVO> shopBrowseVOArrayList_yest = new ArrayList<ShopBrowseVO>();

        for (ShopBrowseDTO dto_yest : shopBrowseDTOList_yest){
            ShopBrowseVO shopBrowseVO_yest = new ShopBrowseVO();
            BeanUtils.copyProperties(dto_yest,shopBrowseVO_yest);
            if (dto_yest.getCommodityId() != null){
                shopBrowseVO_yest.setCommodityPriceVO(iCommodityInfoService.getByIdOtherInterface(dto_yest.getCommodityId()));
                shopBrowseVOArrayList_yest.add(shopBrowseVO_yest);
            }
        }

        map.put("yesterday", shopBrowseVOArrayList_yest);

        //获取更早之前
        wrappern_earlier.lt("create_time", yest_date);//<
        wrappern_earlier.orderBy("create_time", false);//降序
        Page<ShopBrowseDTO> page_earlier = this.selectPage(page, wrappern_earlier);
        List<ShopBrowseDTO> shopBrowseDTOList_earlier =  page_earlier.getRecords();
        ArrayList<ShopBrowseVO> shopBrowseVOArrayList_earlier = new ArrayList<ShopBrowseVO>();

        for (ShopBrowseDTO dto_earlier : shopBrowseDTOList_earlier){
            ShopBrowseVO shopBrowseVO_earlier = new ShopBrowseVO();
            BeanUtils.copyProperties(dto_earlier,shopBrowseVO_earlier);
            if (dto_earlier.getCommodityId() != null){
                shopBrowseVO_earlier.setCommodityPriceVO(iCommodityInfoService.getByIdOtherInterface(dto_earlier.getCommodityId()));
                shopBrowseVOArrayList_earlier.add(shopBrowseVO_earlier);
            }
        }

        map.put("earlier", shopBrowseVOArrayList_earlier);

        return map;
    }

    @Override
    public Page<ShopBrowseVO> queryPageListAll(Page<ShopBrowseVO> page, ShopBrowseBO shopBrowseBO) {
        ArrayList<ShopBrowseVO> shopBrowseVOArrayList = new ArrayList<ShopBrowseVO>();

        Wrapper<ShopBrowseDTO> wrapper = new EntityWrapper<>();
        if(shopBrowseBO.getShopId() != null){
            wrapper.eq("shop_id", shopBrowseBO.getShopId());
        }
        wrapper.orderBy("create_time", false);//降序

        List<ShopBrowseDTO> shopBrowseDTOList = this.selectList(wrapper);

        for (ShopBrowseDTO dto : shopBrowseDTOList){
            ShopBrowseVO shopBrowseVO = new ShopBrowseVO();
            BeanUtils.copyProperties(dto,shopBrowseVO);

            if (dto.getCommodityId() != null){
                shopBrowseVO.setCommodityPriceVO(iCommodityInfoService.getByIdOtherInterface(dto.getCommodityId()));
            }
            shopBrowseVOArrayList.add(shopBrowseVO);
        }

        return page.setRecords(shopBrowseVOArrayList);
    }
}
